################################################################################
################################################################################
################################################################################
# THIS R SCRIPT REPLICATES ALL ANALYSES INCLUDED IN THE MAIN PAPER AND
# SUPPLEMENTAL INFORMATION THAT UTILIZE DATA FROM MY SURVEY OF LOBBYISTS.
# ANALYSES PRESENTED IN ORDER OF APPEARANCE, STARTING WITH THE MAIN PAPER AND
# THEN MOVING TO THE SI.
################################################################################
################################################################################
################################################################################

library(data.table)
library(survey)
library(weights)

# SET WORKING DIRECTORY TO APPROPRIATE LOCATION
# setwd("")

survey_data <- fread("lobbyist_survey_data.csv", stringsAsFactors = FALSE, header = TRUE)

################################################################################
# CREATING SURVEY OBJECTS USING THE "SURVEY" PACKAGE TO WEIGHT RESPONSES TO THE
# FULL SAMPLING FRAME ON OBSERVABLES; AS NOTED IN PAPER/SI, WEIGHTS GENERATED 
# SEPARATELY FOR THOSE RESPONDENTS WHO ANSWERED AT LEAST ONE OF THE QUESTIONS 
# ABOUT THE IMPORTANCE OF LOBBYING DIFFERENCE VENUES (survey_data_import) AND 
# FOR THOSE WHO ANSWERED AT LEAST ONE OF THE QUESTIONS ABOUT FREQUENCY OF DIRECT
# CONTACT WITH THE OBAMA/TRUMP WHITE HOUSES, WHICH WAS THE FIRST SET OF QUESTIONS
# IN THAT MODULE (survey_data_wh)

survey_data_import <- svydesign(ids=~1, 
                                data = survey_data[which(!is.na(survey_data$congress_import)|
                                                         !is.na(survey_data$bureau_import)|
                                                         !is.na(survey_data$wh_import)),])
survey_data_wh <- svydesign(ids=~1, 
                            data = survey_data[which(!is.na(survey_data$obama_not_ip)|
                                                     !is.na(survey_data$obama_ip_wh)|
                                                     !is.na(survey_data$obama_ip_notwh)|
                                                     !is.na(survey_data$trump_not_ip)|
                                                     !is.na(survey_data$trump_ip_wh)|
                                                     !is.na(survey_data$trump_ip_notwh)),])

# IN ORDER TO GENERATE WEIGHTS, CREATING DATA FRAMES WITH PROPORTIONS OF EACH
# OF THE FOUR OBSERVABLE CHARACTERISTICS VIA CRP USING FREQUENCIES IN TABLE
# SI.2; AS ABOVE, DISTRIBUTIONS CALCULATED SEPARATELY FOR EACH SET OF RESPONDENTS

# FIRST FOR RESPONDENTS ANSWERING THE VENUE IMPORTANCE QUESTION

# DISTRIBUTION OF SAMPLING FRAME EMPLOYED BY CLIENTS VS. FIRMS
firm.dist.import <- data.frame(samp_firm = c("0", "1"),
                        Freq = nrow(survey_data_import) * c(0.534, 0.466))

# DISTRIBUTION OF SAMPLING FRAME WHO ARE REGISTERED LOBBYISTS
reglob.dist.import <- data.frame(samp_reglob = c("0", "1"),
                          Freq = nrow(survey_data_import) * c(0.375, 0.625))

# DISTRIBUTION OF SAMPLING FRAME WHOSE EMPLOYERS' EXPENDITURES ARE IN EACH QUARTILE
spend.dist.import <- data.frame(samp_expquart = c("First", "Second", "Third", "Fourth"),
                         Freq = nrow(survey_data_import) * c(0.250, 0.250, 
                                                             0.250, 0.250))

# DISTRIBUTION OF SAMPLING FRAME WHOSE EMPLOYERS ARE CODED AS IN EACH OF CRP'S SECTORS
cat.dist.import <- data.frame(samp_CRPcat = c("Agribusiness", 
                                              "Communications and Electronics",
                                              "Construction", "Defense", 
                                              "Energy and Natural Resources",
                                              "Finance, Insurance and Real Estate",
                                              "Health", 
                                              "Ideological and Single-Issue",
                                              "Labor", "Lawyers and Lobbyists",
                                              "Misc Business", "Other", 
                                              "Transportation", "Unknown"),
                       Freq = nrow(survey_data_import) * c(0.042, 0.066, 0.021, 
                                                           0.021, 0.081, 0.096, 
                                                           0.185, 0.092, 0.021, 
                                                           0.012, 0.118, 0.062,
                                                           0.061, 0.121))

# THEN FOR RESPONDENTS ANSWERING AT LEAST ONE OF THE QUESTIONS AT THE BEGINNING
# OF THE WHITE HOUSE MODULE

# DISTRIBUTION OF SAMPLING FRAME EMPLOYED BY CLIENTS VS. FIRMS
firm.dist.wh <- data.frame(samp_firm = c("0", "1"),
                               Freq = nrow(survey_data_wh) * c(0.534, 0.466))

# DISTRIBUTION OF SAMPLING FRAME WHO ARE REGISTERED LOBBYISTS
reglob.dist.wh <- data.frame(samp_reglob = c("0", "1"),
                                 Freq = nrow(survey_data_wh) * c(0.375, 0.625))

# DISTRIBUTION OF SAMPLING FRAME WHOSE EMPLOYERS' EXPENDITURES ARE IN EACH QUARTILE
spend.dist.wh <- data.frame(samp_expquart = c("First", "Second", "Third", "Fourth"),
                                Freq = nrow(survey_data_wh) * c(0.250, 0.250, 
                                                                0.250, 0.250))

# DISTRIBUTION OF SAMPLING FRAME WHOSE EMPLOYERS ARE CODED AS IN EACH OF CRP'S SECTORS
cat.dist.wh <- data.frame(samp_CRPcat = c("Agribusiness", 
                                          "Communications and Electronics",
                                          "Construction", "Defense", 
                                          "Energy and Natural Resources",
                                          "Finance, Insurance and Real Estate",
                                          "Health", "Ideological and Single-Issue",
                                          "Labor", "Lawyers and Lobbyists",
                                          "Misc Business", "Other", 
                                          "Transportation", "Unknown"),
                              Freq = nrow(survey_data_wh) * c(0.042, 0.066, 
                                                              0.021, 0.021,
                                                              0.081, 0.096, 
                                                              0.185, 0.092,
                                                              0.021, 0.012, 
                                                              0.118, 0.062,
                                                              0.061, 0.121))

# RAKING THE GENERATE SURVEY WEIGHTS

survey_data_import.rake <- rake(design = survey_data_import,
                                    sample.margins = list(~samp_firm, 
                                                          ~samp_reglob,
                                                          ~samp_expquart, 
                                                          ~samp_CRPcat),
                                    population.margins = list(firm.dist.import, 
                                                              reglob.dist.import,
                                                              spend.dist.import, 
                                                              cat.dist.import))

survey_data_wh.rake <- rake(design = survey_data_wh,
                                   sample.margins = list(~samp_firm, 
                                                         ~samp_reglob,
                                                         ~samp_expquart, 
                                                         ~samp_CRPcat),
                                   population.margins = list(firm.dist.wh, 
                                                             reglob.dist.wh,
                                                             spend.dist.wh, 
                                                             cat.dist.wh))

################################################################################
# PAGE 4, FOOTNOTE 9 (ALSO PAGE SI.9, IN-TEXT)
# CORRELATIONS AMONG MEDIUMS OF WHITE HOUSE ENGAGEMENT

# Obama, mail/email/phone vs. in-person at WH
wtd.cor(x = survey_data_import.rake$variables$obama_not_ip, 
        y = survey_data_import.rake$variables$obama_ip_wh, 
        w = survey_data_import.rake$prob) 

# Obama, in-person at WH vs. in-person not at WH
wtd.cor(x = survey_data_import.rake$variables$obama_ip_wh, 
        y = survey_data_import.rake$variables$obama_ip_notwh, 
        w = survey_data_import.rake$prob) 

# Trump, mail/email/phone vs. in-person at WH
wtd.cor(x = survey_data_import.rake$variables$trump_not_ip, 
        y = survey_data_import.rake$variables$trump_ip_wh, 
        w = survey_data_import.rake$prob) 

# Trump, in-person at WH vs. in-person not at WH
wtd.cor(x = survey_data_import.rake$variables$trump_ip_wh, 
        y = survey_data_import.rake$variables$trump_ip_notwh, 
        w = survey_data_import.rake$prob) 

################################################################################
# PAGE 8, FIGURE 1
# IMPORTANCE OF DIRECT CONTACTS ACROSS INSTITUTIONS

# BELOW CODE WILL SAVE FIGURE TO YOUR CURRENT WD

# FIGURE CODE USES THE "svytable" FUNCTION IN THE WEIGHTS PACKAGE TO CALCULATE
# THE PROPORTION OF RESPONDENTS OFFERING EACH RESPONSE OPTION FOR EACH QUESTION

pdf(file = "venue-importance.pdf", family = "Times", height = 5, width = 12)
layout(matrix(c(1,1,1,2,3,4), nrow=2, byrow=TRUE), 
       heights = c(0.15, 0.85), widths = c(1/3, 1/3, 1/3))
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "How important are direct contacts with...", cex=3)
par(mar=c(6,6,2,1))
barplot(svytable(~congress_import, survey_data_import.rake, Ntotal = 100), 
        main="Congress?", font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,80),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2,
        axisnames = FALSE)
axis(2,at=c(0, 20, 40, 60, 80), 
     labels = c("0", "20", "40", "60", "80"), cex.axis = 2)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Not at all\nimportant", "Slightly\nimportant", "Somewhat\nimportant", 
                "Very\nimportant", "Extremely\nimportant"),
     adj = 1, cex=1.25, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~congress_import, survey_data_import.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~congress_import, 
                                          survey_data_import.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
barplot(svytable(~bureau_import, survey_data_import.rake, Ntotal = 100), 
        main="the Federal Bureaucracy?", font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,80),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2,
        axisnames = FALSE)
axis(2,at=c(0, 20, 40, 60, 80), 
     labels = c("0", "20", "40", "60", "80"), cex.axis = 2)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Not at all\nimportant", "Slightly\nimportant", "Somewhat\nimportant", 
                "Very\nimportant", "Extremely\nimportant"),
     adj = 1, cex=1.25, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~bureau_import, survey_data_import.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~bureau_import, 
                                          survey_data_import.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
barplot(svytable(~wh_import, survey_data_import.rake, Ntotal = 100), 
        main="the White House?", font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,80),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2,
        axisnames = FALSE)
axis(2,at=c(0, 20, 40, 60, 80), 
     labels = c("0", "20", "40", "60", "80"), cex.axis = 2)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Not at all\nimportant", "Slightly\nimportant", "Somewhat\nimportant", 
                "Very\nimportant", "Extremely\nimportant"),
     adj = 1, cex=1.25, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~wh_import, survey_data_import.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~wh_import, 
                                          survey_data_import.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)

dev.off()

# N OF RESPONDENTS PER QUESTION
sum(table(survey_data_import.rake$variables$congress_import))
sum(table(survey_data_import.rake$variables$bureau_import))
sum(table(survey_data_import.rake$variables$wh_import))

################################################################################
# PAGE 9, FIGURE 2
# WHO INITIATES ENGAGEMENT/HOW OFTEN DOES THE WH GRANT REQUESTS/HOW OFTEN DO
# LOBBYISTS ACCEPT WH INVITATIONS?

# FIGURE CODE USES THE "svytable" FUNCTION IN THE WEIGHTS PACKAGE TO CALCULATE
# THE PROPORTION OF RESPONDENTS OFFERING EACH RESPONSE OPTION FOR EACH QUESTION

pdf(file = "obama-trump-initiate-accept.pdf", family = "Times", height = 15, 
    width = 15)

layout(matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, byrow=TRUE), 
       widths = c(0.18, 0.41, 0.41), heights = c(0.07, 0.31, 0.31, 0.31))
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Obama Administration", cex=4)
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Trump Administration", cex=4)
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Who Initiates,\n Your Organization\n or the White House?", 
     cex=2.5)
par(mar=c(8,6,2,1))
barplot(svytable(~obama_initiate, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, 
        cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Always\nOrg", "Usually\nOrg", "Sometimes WH\nSometimes Org", 
                "Usually\nWH", "Always\nWH"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~obama_initiate, survey_data_wh.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~obama_initiate, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 2.25)
barplot(svytable(~trump_initiate, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, 
        cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Always\nOrg", "Usually\nOrg", "Sometimes WH\nSometimes Org", 
                "Usually\nWH", "Always\nWH"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~trump_initiate, survey_data_wh.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_initiate, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 2.25)


par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "How Often Does the\n White House Grant\n Your Requests?", 
     cex=2.5)
par(mar=c(8,6,2,1))
barplot(svytable(~obama_grant, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, 
        cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
# mtext(text = c("Never", "Rarely", "Sometimes", "Usually", "Always"),
#       side=1, at=c(0.7,1.9,3.1,4.3,5.5), line=2.5, cex=1.25)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Never", "Rarely", "Sometimes", "Usually", "Always"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~obama_grant, survey_data_wh.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~obama_grant, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 2.25)
barplot(svytable(~trump_grant, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, 
        cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Never", "Rarely", "Sometimes", "Usually", "Always"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~trump_grant, survey_data_wh.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_grant, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 2.25)

par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "How Often Do You\n Accept White House\n Invitations?", cex=2.5)
par(mar=c(8,6,2,1))
barplot(c(svytable(~obama_accept, survey_data_wh.rake, Ntotal = 100)[1], 0,
          svytable(~obama_accept, survey_data_wh.rake, Ntotal = 100)[2:4]), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, 
        cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Never", "Rarely", "Sometimes", "Usually", "Always"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     c(svytable(~obama_accept, survey_data_wh.rake, Ntotal = 100)[1], 0, 
       svytable(~obama_accept, survey_data_wh.rake, Ntotal = 100)[2:4]) + 3.5, 
     c(paste0(sprintf("%.1f",round(svytable(~obama_accept, survey_data_wh.rake, 
                                            Ntotal = 100), 1)),"%")[1], 
       "0.0%", paste0(sprintf("%.1f",round(svytable(~obama_accept, 
                                                    survey_data_wh.rake, 
                                                    Ntotal = 100), 1)),"%")[2:4]), 
     cex = 2.25, xpd = TRUE)
barplot(c(svytable(~trump_accept, survey_data_wh.rake, Ntotal = 100)), 
        main=NULL, font.main = 1, cex.main = 2,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,75),
        yaxt = "n",
        col = c("gray90", "gray70", "gray50", "gray30", "gray10"),
        beside = TRUE, cex.axis = 2.5, cex.names = 2.5, cex.lab = 2.5, cex.main = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 25, 50, 75), 
     labels = c("0", "25", "50", "75"), cex.axis = 2.5)
text(x = c(1.1,2.3,3.5,4.7,5.9), y = par("usr")[3] - 5,
     labels = c("Never", "Rarely", "Sometimes", "Usually", "Always"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5),
     svytable(~trump_accept, survey_data_wh.rake, Ntotal = 100) + 3.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_accept, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 2.25)
dev.off()

# N OF RESPONDENTS PER QUESTION
sum(table(survey_data_wh.rake$variables$obama_initiate))
sum(table(survey_data_wh.rake$variables$trump_initiate))
sum(table(survey_data_wh.rake$variables$obama_grant))
sum(table(survey_data_wh.rake$variables$trump_grant))
sum(table(survey_data_wh.rake$variables$obama_accept))
sum(table(survey_data_wh.rake$variables$trump_accept))

################################################################################
# PAGE SI.3, IN-TEXT
# RESPONSE RATE FOR THOSE ANSWERING GENERAL QUESTIONS ABOUT EXPERIENCE WORKING 
# FOR THEIR CURRENT CLIENT AND FOR INTERACTIONS WITH THE OBAMA/TRUMP WHITE
# HOUSES

# ABOVE, WEIGHTS WERE GENERATED FOR THOSE RESPONDENTS WHO REACHED EACH MODULE,
# SEPARATELY, SO WE NEED ONLY TAKE THE NUMBER OF OBSERVATIONS FOR WHICH WEIGHTS
# WERE GENERATED AND DIVIDE BY THE SIZE OF THE SAMPLING FRAME (5458)

# RESPONSE RATE FOR THOSE ANSWERING GENERAL QUESTIONS ABOUT EXPERIENCE WORKING
# FOR THEIR CURRENT CLIENT

round(length(survey_data_import.rake$prob)/5458, 3)*100

# RESPONSE RATE FOR THOSE ANSWERING GENERAL QUESTIONS ABOUT CLIENT'S INTERACTIONS
# WITH OBAMA/TRUMP WHITE HOUSES

round(length(survey_data_wh.rake$prob)/5458, 3)*100

################################################################################
# PAGES SI.3-4, IN-TEXT
# PROPORTION OF RESPONDENTS WHO REPORTED/DID NOT REPORT THAT THEIR CLIENT
# ATTENDED AT LEAST ONE WHITE HOUSE MEETING DURING THE OBAMA/TRUMP
# ADMINISTRATIONS

# OBAMA ADMINISTRATION; ANY VALUES HIGHER THAN 0 INDICATE AT LEAST ONE VISIT
table(survey_data_wh.rake$variables$obama_ip_wh==0)
round(table(survey_data_wh.rake$variables$obama_ip_wh==0)/
        sum(table(survey_data_wh.rake$variables$obama_ip_wh==0)), 3)*100

# TRUMP ADMINISTRATION; ANY VALUES HIGHER THAN 0 INDICATE AT LEAST ONE VISIT
table(survey_data_wh.rake$variables$trump_ip_wh==0)
round(table(survey_data_wh.rake$variables$trump_ip_wh==0)/
        sum(table(survey_data_wh.rake$variables$trump_ip_wh==0)), 3)*100

################################################################################
# PAGE SI.4, IN-TEXT
# PROPORTION OF RESPONDENTS WHO SAID THEY DID/DID NOT EXPERIENCE ENGAGEMENT AS
# COMPARED TO WHETHER THEIR CLIENT EXPERIENCED ENGAGEMENT IN THE OBAMA WH
# VISITOR LOGS

# FIRST VARIABLE IS A DICHOTOMOUS INDICATOR OF WHETHER I DETECTED THEIR CLIENT
# EXPERIENCING ENGAGEMENT AT LEAST ONCE IN THE 7 YEAR PERIOD FOR WHICH I HAVE
# VISITOR LOGS, SECOND VARIABLE IS A LOGICAL WHICH IS TRUE IF THE RESPONDENT
# INDICATED THAT THEIR CLIENT HAD FEWER THAN 5 (BUT MORE THAN ZERO) IN-PERSON WH
# MEETINGS IN AN AVERAGE YEAR DURING THE OBAMA WH; VALUES ON THE DIAGONALS 
# CORRESPOND TO RESPONDENTS WHOSE SELF-REPORTS OF ENGAGEMENT MATCH WITH MY 
# ANALYSIS OF THE OBAMA VISITOR LOGS, VALUES ON THE OFF-DIAGONALS ARE MISMATCHES

table(survey_data$obama_whlogs_access, survey_data$obama_ip_wh>0)
round(prop.table(table(survey_data$obama_whlogs_access, survey_data$obama_ip_wh>0)),3)*100

################################################################################
# PAGE SI.4, FOOTNOTE SI.5
# TEST STATISTICS FOR COMPARISONS OF RESPONDENTS WITH SAMPLING FRAME

# EMPLOYER TYPE
t.test(survey_data$samp_firm[which(!is.na(survey_data$ResponseID))],
       survey_data$samp_firm[which(is.na(survey_data$ResponseID))])

# LOBBYING EXPENDITURES
chisq.test(table(survey_data$samp_expquart, !is.na(survey_data$ResponseID)))

# CRP CATEGORY
chisq.test(table(survey_data$samp_CRPcat, !is.na(survey_data$ResponseID)))

# REGISTERED LOBBYIST
t.test(survey_data$samp_reglob[which(!is.na(survey_data$ResponseID))],
       survey_data$samp_reglob[which(is.na(survey_data$ResponseID))])

################################################################################
# PAGE SI.5, TABLE SI.2
# COMPARISON OF RESPONDENTS WITH SAMPLING FRAME

# EMPLOYER TYPE
# IN SAMPLING FRAME
table(survey_data$samp_firm)
round(prop.table(table(survey_data$samp_firm)), 3)*100

# AMONG RESPONDENTS
table(survey_data$samp_firm[!is.na(survey_data$ResponseID)])
round(prop.table(table(survey_data$samp_firm[!is.na(survey_data$ResponseID)])), 3)*100

# LOBBYING EXPENDITURES
# IN SAMPLING FRAME
table(survey_data$samp_expquart)
round(prop.table(table(survey_data$samp_expquart)), 3)*100

# AMONG RESPONDENTS
table(survey_data$samp_expquart[!is.na(survey_data$ResponseID)])
round(prop.table(table(survey_data$samp_expquart[!is.na(survey_data$ResponseID)])), 3)*100

# CRP CATEGORY
# IN SAMPLING FRAME
table(survey_data$samp_CRPcat)
round(prop.table(table(survey_data$samp_CRPcat)), 3)*100

# AMONG RESPONDENTS
table(survey_data$samp_CRPcat[!is.na(survey_data$ResponseID)])
round(prop.table(table(survey_data$samp_CRPcat[!is.na(survey_data$ResponseID)])), 3)*100

# REGISTERED LOBBYIST
# IN SAMPLING FRAME
table(survey_data$samp_reglob)
round(prop.table(table(survey_data$samp_reglob)), 3)*100

# AMONG RESPONDENTS
table(survey_data$samp_reglob[!is.na(survey_data$ResponseID)])
round(prop.table(table(survey_data$samp_reglob[!is.na(survey_data$ResponseID)])), 3)*100

################################################################################
# PAGE SI.6, TABLE SI.3
# DESCRIPTIVE STATISTICS OF RESPONDENTS

# GENDER
table(survey_data$resp_gender[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_gender[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# AGE
table(survey_data$resp_age[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_age[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# EDUCATION
table(survey_data$resp_educ[which(!is.na(survey_data$ResponseID))],
      useNA = "always")
round(prop.table(table(survey_data$resp_educ[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# RACE
table(survey_data$resp_race[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_race[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# INCOME
table(survey_data$resp_income[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_income[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# IDEOLOGY
table(survey_data$resp_ideo[which(!is.na(survey_data$ResponseID))], useNA = "always")
round(prop.table(table(survey_data$resp_ideo[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# PID
table(survey_data$resp_pid[which(!is.na(survey_data$ResponseID))], useNA = "always")
round(prop.table(table(survey_data$resp_pid[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# YEARS OF LOBBYING EXPERIENCE
table(survey_data$resp_years_exp[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_years_exp[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# PAST GOVERNMENT EXPERIENCE (NOTE: RESPONDENTS COULD SELECT ALL THAT APPLY)
# MEMBER OF CONGRESS
table(survey_data$resp_prevexp_moc[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_moc[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# CONGRESSIONAL STAFF
table(survey_data$resp_prevexp_congstaff[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_congstaff[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# PRESIDENTIAL APPOINTEE
table(survey_data$resp_prevexp_presappt[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_pressappt[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# EOP STAFFER
table(survey_data$resp_prevexp_eop[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_eop[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# CIVIL SERVANT
table(survey_data$resp_prevexp_bureaucrat[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_bureaucrat[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# OTHER
table(survey_data$resp_prevexp_other[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_other[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100
# NO EXPERIENCE (I.E., SELECTED NONE OF THE OPTIONS)
table(survey_data$resp_prevexp_none[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_prevexp_none[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

# CURRENT ROLE WITH CLIENT
table(survey_data$resp_curr_role[which(!is.na(survey_data$ResponseID))], 
      useNA = "always")
round(prop.table(table(survey_data$resp_curr_role[which(!is.na(survey_data$ResponseID))], 
                       useNA = "always")),3)*100

################################################################################
# PAGE SI.14, FIGURE SI.1
# RELATIONSHIPS BETWEEN MEDIUMS OF ENGAGEMENT

# FIGURE CODE USES THE "svytable" FUNCTION IN THE WEIGHTS PACKAGE TO CALCULATE
# THE PROPORTION OF RESPONDENTS OFFERING EACH RESPONSE OPTION FOR EACH QUESTION

pdf(file = "obama-trump-engagement-mediums.pdf", family = "Times", height = 15, 
    width = 15)
layout(matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=4, byrow=TRUE), 
       widths = c(0.18, 0.41, 0.41), heights = c(0.07, 0.31, 0.31, 0.31))
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Obama Administration", cex=4)
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Trump Administration", cex=4)
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Direct contacts via\n mail, email,\n or phone", cex=2.5)
par(mar=c(8,6,2,1))
barplot(svytable(~obama_not_ip, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~obama_not_ip, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~obama_not_ip, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
par(mar=c(8,6,2,1))
barplot(svytable(~trump_not_ip, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~trump_not_ip, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_not_ip, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Direct contacts via\n meetings at\n White House", cex=2.5)
par(mar=c(8,6,2,1))
barplot(svytable(~obama_ip_wh, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~obama_ip_wh, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~obama_ip_wh, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
par(mar=c(8,6,2,1))
barplot(svytable(~trump_ip_wh, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~trump_ip_wh, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_ip_wh, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
text(x=0.03, y=0, "Direct contacts via\n meetings outside\n White House", cex=2.5)
par(mar=c(8,6,2,1))
barplot(svytable(~obama_ip_notwh, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~obama_ip_notwh, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~obama_ip_notwh, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
par(mar=c(8,6,2,1))
barplot(svytable(~trump_ip_notwh, survey_data_wh.rake, Ntotal = 100), 
        main=NULL, font.main = 1, cex.main = 2.5,
        xlab = NULL,
        ylab = "% of Respondents",
        ylim = c(0,60),
        yaxt = "n",
        col = c("gray95", "gray80", "gray65", "gray50", "gray35", "gray20"),
        beside = TRUE, cex.axis = 2, cex.names = 2, cex.lab = 2.5,
        axisnames = FALSE)
axis(2,at=c(0, 20,  40, 60), 
     labels = c("0", "20", "40", "60"), cex.axis = 2)
text(x = c(0.7,1.9,3.1,4.3,5.5,6.7), y = par("usr")[3] - 5,
     labels = c("Never", "< 5", "5-10", 
                "11-15", "16-20", "> 20"),
     adj = 1, cex=1.75, srt = 35, xpd =TRUE)
text(c(0.7,1.9,3.1,4.3,5.5,6.7),
     svytable(~trump_ip_notwh, survey_data_wh.rake, Ntotal = 100) + 2.5, 
     paste0(sprintf("%.1f",round(svytable(~trump_ip_notwh, survey_data_wh.rake, 
                                          Ntotal = 100), 1)),"%"), cex = 1.75)
dev.off()

# N OF RESPONDENTS PER QUESTION
sum(table(survey_data_wh.rake$variables$obama_not_ip))
sum(table(survey_data_wh.rake$variables$trump_not_ip))
sum(table(survey_data_wh.rake$variables$obama_ip_wh))
sum(table(survey_data_wh.rake$variables$trump_ip_wh))
sum(table(survey_data_wh.rake$variables$obama_ip_notwh))
sum(table(survey_data_wh.rake$variables$trump_ip_notwh))

################################################################################
# PAGE SI.10, IN TEXT
# REASONS FOR NOT EXPERIENCING ENGAGEMENT

# FREQUENCY TABLES OF RESPONDENTS INDICATING WHETHER THEY TURNED DOWN WH INVITES
# OBAMA
table(survey_data$obama_turneddown)
# TRUMP
table(survey_data$trump_turneddown)

# FREQUENCY TABLES OF RESPONDENTS INDICATING WHETHER THEY REQUESTED A MEETING
# AND WERE TURNED DOWN BY THE WH
# OBAMA
table(survey_data$obama_refused)
# TRUMP
table(survey_data$trump_refused)

################################################################################
# PAGE SI.20, IN TEXT
# TYPES OF ORGANIZED INTEREST REPRESENTATIVES ATTENDING WH MEETINGS, AS REPORTED
# BY RESPONDENTS

# OBAMA
# REGISTERED LOBBYISTS
round(svytable(~obama_attendees_reglobby, survey_data_wh.rake, Ntotal = 100),1)
# EXECUTIVE OFFICERS
round(svytable(~obama_attendees_execs, survey_data_wh.rake, Ntotal = 100),1)

# TRUMP
# REGISTERED LOBBYISTS
round(svytable(~trump_attendees_reglobby, survey_data_wh.rake, Ntotal = 100),1)
# EXECUTIVE OFFICERS
round(svytable(~trump_attendees_execs, survey_data_wh.rake, Ntotal = 100),1)